<?php
include_once BONE_ROOT.DS.APP_DIR.DS.'templates'.DS.'content'.DS.'content.php';

class template_category_listing extends template_content
{
	

	protected function head()
	{
	$category_list = $this->get('category_list');
	?>
<script type="text/javascript" language="javascript" src="templates/content/js/category_listing.js"></script>
<script type="text/javascript" language="javascript">
var g_oCategoryChain = new Object();
var g_iCategoryChainHead = 0;
<?php

if(count( $category_list ))
{
	// 构造双向链表保存菜单项， 方便 JS 查找.
	$pre_id = 0;
	$this_id = 0;
	$next_id = 0;
	
	$category = null;
	foreach($category_list as $cat)
	{
		if($category)
		{
			$pre_id = $this_id;
			$this_id = $category->id;
			$next_id = $cat->id;
			echo 'g_oCategoryChain["'.$category->id.'"]={"pre_id":'.$pre_id.',"next_id":'.$next_id.',"id":'.$category->id.',"name":"'.$category->name.'","parent_id":'.$category->parent_id.',"level":'.$category->level.',"children":'.$category->children.'};'."\r\n";
		}
		else
			echo 'g_iCategoryChainHead = '.$cat->id.";\r\n";
		$category = $cat;
	}
	echo 'g_oCategoryChain["'.$category->id.'"]={"pre_id":'.$this_id.',"next_id":0,"id":'.$category->id.',"name":"'.$category->name.'","parent_id":'.$category->parent_id.',"level":'.$category->level.',"children":'.$category->children.'};'."\r\n";
}
?>
</script>
	<?php
	}

	protected function center()
	{
		
		$category_list = $this->get('category_list');
		$this->toolbar();
		?>

<div class="box">
	<div class="box-tl"><div class="box-tr"><div class="box-tm"></div></div></div>	
	<div class="box-m">

		<h3 class="title"><?php echo $this->get_title(); ?></h3>
		
		<form action="./" method="post">
		<table class="list-table">
		
		<thead>
		<tr>
			<th width="80">添加子分类</th><th width="30">收起</th><th>分类名称</th><th width="30">上移</th><th width="30">下移</th><th width="40">删除</th>
		</tr>
		</thead>
		
		<tbody id="row-list">
		<?php 
		if(count($category_list))
		{
			foreach($category_list as $category)
			{
			
			?>
			<tr id="row-<?php echo $category->id; ?>"  onMouseOver="this.className='row-on'" onMouseOut="this.className='row-off'">
			<td align="center"><a href="javascript:;" onclick="javascript:addSubCategory(<?php echo $category->id; ?>)" class="add"></a></td>
			<td align="center">
			<?php
			echo '<a href="javascript:;" id="toggle-'.$category->id.'" onclick="javascript:togleSubCategory('.$category->id.')" class="toggle-on"';
			if(!$category->children)
			{
				echo ' style="display:none;"';
			}
			echo '></a>';

			?>
			</td>
			<td>
			<?php
			if($category->level)
			{
				echo str_repeat('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ', $category->level);
				echo '|_ ';
			}
			?>
			<input type="hidden" name="id[]" id="id-<?php echo $category->id; ?>" value="<?php echo $category->id; ?>" />
			<input type="hidden" name="parent_id[]" id="parent_id-<?php echo $category->id; ?>" value="<?php echo $category->parent_id; ?>" />
			<input type="text" name="name[]" id="name-<?php echo $category->id; ?>"  value="<?php echo $category->name; ?>" size="50" maxlength="120" />
			</td>
			<td align="center"><a href="javascript:;" onclick="javascript:orderUp(<?php echo $category->id; ?>)" class="order-up-on" id="order-up-<?php echo $category->id; ?>"></a></td>
			<td align="center"><a href="javascript:;" onclick="javascript:orderDown(<?php echo $category->id; ?>)" class="order-down-on" id="order-down-<?php echo $category->id; ?>"></a></td>
			<td align="center"><a href="javascript:;" onclick="javascript:confirmDeleteCategory(<?php echo $category->id; ?>)" class="delete"></a></td>
			</tr>		
			<?php
			}
		}
		else
		{
		?>
		<tr>
			<td colspan="8">没有记录</td>
		</tr>
		<?php
		}
		?>
		</tbody>
		
		<tfoot>
		<tr>
			<td><input type="button" value="添加新分类" onclick="javascript:addSubCategory(0)"/></td>
			<td colspan="7">
			<div class="align-left"><input type="submit" value="保存" /></div>
			</td>
		</tr>
		</tfoot>
		</table>
		<input type="hidden" name="controller" value="content" />
		<input type="hidden" name="task" value="category_listing_save" />
		</form>

	</div>
	<div class="box-bl"><div class="box-br"><div class="box-bm"></div></div></div>
</div>
		

		<?php
		
	}	

}
?>